package com.zmn.oms.third.gomegj.service.impl;

import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.third.gomegj.GmgjConsts;
import com.zmn.oms.third.gomegj.service.GmgjService;
import com.zmn.oms.zmn.business.interfaces.open.OrderChangeNotifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 类描述：同步渠道订单状态变化实现类
 *
 * @author XieWenBing
 * @date 2020/11/05 18:00
 */

@Slf4j
@Service("gmgjNotifyService")
public class GmgjNotifyServiceImpl implements OrderChangeNotifyService {

    @Autowired
    protected GmgjService gmgjService;

    @Autowired
    private OrderWorkService orderWorkService;

    @Override
    public void notifyChange(OrderWorkLogChange orderWorkLogChange) {

        // 判断功能是否开启
        if (!GmgjConsts.POWER_ON) {
            log.info("【{}】<订单状态同步> 该功能没有开启。", GmgjConsts.CHANNEL_NAME);
            return;
        }

        // 参数判断
        if (Objects.isNull(orderWorkLogChange)) {
            log.info("【{}】<订单状态同步> - orderWorkLogChange参数为空。", GmgjConsts.CHANNEL_NAME);
            return;
        }

        if (!Objects.equals(orderWorkLogChange.getInputType(),OrderConsts.ORDER_INPUT_TYPE_API)) {
            log.info("【{}】的录入方式不为API接入，不需要状态同步", GmgjConsts.CHANNEL_NAME);
            return;
        }

        log.info("【{}】<订单状态同步> ,状态信息:[{}]。", GmgjConsts.CHANNEL_NAME,orderWorkLogChange.toString());

        int logType = orderWorkLogChange.getLogType().intValue();

        try{
            switch (logType) {

                // 修改/确认 - 改预约时间
                case OrderLogConsts.ORDER_LOG_TYPE_UPDATE:
                case OrderLogConsts.ORDER_LOG_TYPE_CONFIRM:
                case OrderLogConsts.ORDER_LOG_TYPE_MODIFY_DUTY_TIME:
                case OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME:
                    gmgjService.syncOrderDutytime(orderWorkLogChange);
                    break;

                // 派单
                case OrderLogConsts.ORDER_LOG_TYPE_DISTRIBUTE:
                    gmgjService.syncOrderDistribute(orderWorkLogChange);
                    break;
                case OrderLogConsts.ORDER_LOG_TYPE_SERVICEITEM:
                    // 修改服务项
                    this.syncOrderServiceItem(orderWorkLogChange);
                    break;

                // 取消、失败、弃单
                case OrderLogConsts.ORDER_LOG_TYPE_CANCEL:
                case OrderConsts.ORDER_OP_TYPE_FAIL:
                case OrderConsts.ORDER_OP_TYPE_DISCARD:
                    gmgjService.syncCancelOrder(orderWorkLogChange);
                    break;

                case OrderLogConsts.ORDER_LOG_TYPE_COMPLETE:
                    gmgjService.syncOrderComplete(orderWorkLogChange);
                    break;

                default:
                    log.info("【{}】<订单状态-【{}】不同步> ", GmgjConsts.CHANNEL_NAME,OrderLogConsts.getOrderLogTypeName(logType));
                    return;
            }
        } catch (OmsBaseException e) {
            log.info("【{}】<订单状态同步>失败，订单状态为:[{}]，提示消息:[{}]。", GmgjConsts.CHANNEL_NAME,
                    OrderLogConsts.getOrderLogTypeName(logType), e.getMessage());
        }

        log.info("【{}】<订单状态【{}】同步完成> ", GmgjConsts.CHANNEL_NAME, OrderLogConsts.getOrderLogTypeName(logType));
    }

    /**
     * 同步服务项
     * @param orderWorkLogChange
     * @throws OmsBaseException
     */
    protected void syncOrderServiceItem(OrderWorkLogChange orderWorkLogChange) throws OmsBaseException {

        // 订单查找
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderWorkLogChange.getOrderId(),
                orderWorkLogChange.getWorkId());
        if (Objects.isNull(orderWork)) {
            throw new OmsBaseException("查找不到对应的订单");
        }
        String accessToken = gmgjService.getAccessToken();
        log.info("国美修改服务项调用同步服务项接口入参,accessToken:{},orderWork:{}", accessToken, orderWork);
        gmgjService.syncOrderServiceItem(accessToken, orderWork);
    }
}
